package businesslogic.ChooseCourseBl;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Iterator;

import businesslogic.CourseBl.Course;
import businesslogic.StudentBl.Student;
import dataservice.ChooseCourseDataService;
import appendix.ResultMessage;
import ClientHelper.ClientHelper;
import po.*;
import vo.ChooseCourseVO;

public class ChooseCourse {

	private ChooseCoursePO record;
	private ChooseCourseDataService chooseCourseData;

	public ChooseCourse() {
		chooseCourseData = ClientHelper.getChooseCourseData();
	}

	public ResultMessage<?> chooseCourse(int chance,long studentID, long courseID) {
		record = new ChooseCoursePO(chance,studentID, courseID);
		ResultMessage<?> rm = new ResultMessage<Boolean>(false);
		try {
			rm = chooseCourseData.insert(record);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rm;
	}

	public ResultMessage<?> chooseCourse(String department, long courseID) {
		Boolean rm = true;
		int chance = 0;
		Student studentHelper = new Student();
		ArrayList<StudentPO> allList = (ArrayList<StudentPO>)studentHelper.getList().result;
		for (StudentPO student: allList) {
			if (student.getDepartment().equals(department)) {
				rm = rm && (Boolean)chooseCourse(chance, student.getID(), courseID).result;
			}
		}
		return new ResultMessage<Boolean>(rm);

	}

	public ResultMessage<?> quitCourse(long studentID, long courseID) {
		ResultMessage<?> rm = new ResultMessage<Boolean>(false);
		record = null;
		try {
			record = (ChooseCoursePO) chooseCourseData
					.find(courseID, studentID).result;
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		if (record != null) {
			try {
				rm = chooseCourseData.delete(record);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return rm;
	}

	public ResultMessage<?> setStudentScores(ChooseCoursePO po) {
		record = po;
		ResultMessage<?> rm = new ResultMessage<Boolean>(false);
		try {
			rm = chooseCourseData.update(record);
			if ((Boolean) rm.result == true) {
				Student studentHelper = new Student();
				Course courseHelper = new Course();
				StudentPO student = (StudentPO) studentHelper
						.getStudent(po.getStudentID()).result;
				CoursePO course = courseHelper.getCourseInfo(po.getCourseID());
				double op = student.getOpCredit();
				double pro = student.getProCredit();
				double GPA = student.getGPA();
				double credit = course.getCredit();
				GPA = GPA * (pro);
				if (course.isCompulsory()) {
					pro += credit;
				} else {
					op += credit;
				}
				GPA = (GPA + credit * po.getScore() / 20) / pro;
				student.setGPA(GPA);
				student.setOpCredit((int) op);
				student.setProCredit((int) pro);
				student.setIsGraduate((op >= 14) && (pro >= 50));
				studentHelper.updateStudent(student);
			}
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rm;
	}

	public ResultMessage<?> searchCourseList(long studentID) {
		Course courseServer = new Course();
		ArrayList<CoursePO> courseList = new ArrayList<CoursePO>();
		ArrayList<ChooseCoursePO> list = new ArrayList<ChooseCoursePO>();
		try {
			list = (ArrayList<ChooseCoursePO>) chooseCourseData.finds().result;
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (list != null) {
			for (ChooseCoursePO tempRecord : list) {
				if (tempRecord.getStudentID() == studentID) {
					CoursePO tempCourse = null;
					tempCourse = courseServer.getCourseInfo(tempRecord
							.getCourseID());
					if (tempCourse != null) {
						courseList.add(tempCourse);
					}
				}
			}
		}

		return new ResultMessage<ArrayList<CoursePO>>(courseList);
	}

	public ResultMessage<?> showScore(long studentID, long courseID) {
		record = null;
		try {
			record = (ChooseCoursePO) chooseCourseData
					.find(courseID, studentID).result;
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if ((record != null) && (record.getScore() != -1)) {
			return new ResultMessage<Integer>(record.getScore());
		}
		return null;
	}

	public ResultMessage<?> searchStudentList(long courseID) {
		Student student = new Student();
		ArrayList<ChooseCourseVO> list = new ArrayList<ChooseCourseVO>();
		ArrayList<ChooseCoursePO> allList = null;
		StudentPO studentPO = null;
		try {
			allList = (ArrayList<ChooseCoursePO>) chooseCourseData.finds().result;
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (allList != null) {
			for (ChooseCoursePO tempRecord : allList) {
				studentPO = (StudentPO) student.getStudent(tempRecord
						.getStudentID()).result;
				if (studentPO != null) {
					list.add(new ChooseCourseVO(tempRecord, studentPO));
				}
			}
		}
		return new ResultMessage<ArrayList<ChooseCourseVO>>(list);
	}
	
	public ResultMessage<?> getChooseOpCourse(long id){
		ArrayList<CoursePO> arr=(ArrayList<CoursePO>)searchCourseList(id).result;
		ArrayList<CoursePO> result=new ArrayList<CoursePO>();
		Iterator<CoursePO> it=arr.iterator();
		while(it.hasNext()){
			CoursePO po=it.next();
			if(!po.isCompulsory()){
				result.add(po);
			}
		}
		return new ResultMessage<ArrayList<CoursePO>>(result);
	}

	public ResultMessage<?> conflictExamForStudent(long id) {
		boolean result = false;
		ArrayList<CoursePO> list = (ArrayList<CoursePO>)searchCourseList(id).result;
		int length = list.size();
		if (length > 2) {
			for (int i = 0; i < length; i++) {
				for (int j = (i + 1); j < length;) {
					if (list.get(i).isConflict(list.get(j))) {
						result = true;
					}
				}
			}
		}
		return new ResultMessage<Boolean>(result);
	}
}
